package com.example.videoserver.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.lang.UUID;
import cn.hutool.json.JSONUtil;
import com.example.videoserver.entity.Button;
import com.example.videoserver.entity.VideoEntity;
import com.example.videoserver.utli.ResponseRest;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RequestMapping("/video")
@RestController()
public class VideController {

    @PostMapping("/save")
    public ResponseRest save(@RequestBody VideoEntity videoEntity){
        videoEntity.setId(UUID.fastUUID().toString());
        System.out.println(videoEntity.toString());

        FileReader reader = new FileReader("/config/video.properties");
        System.out.println(reader.readString());

        List<VideoEntity> videoEntityList = JSONUtil.toList((reader.readString()), VideoEntity.class);

        videoEntityList.add(videoEntity);

        FileWriter writer = new FileWriter("/config/video.properties");
        writer.write(JSONUtil.toJsonStr(videoEntityList));


        return ResponseRest.success();
    }

    @GetMapping("/query")
    public Object query() {
        if (!FileUtil.exist("/config/video.properties")) {
            FileUtil.touch ("/config/video.properties");
            FileWriter writer = new FileWriter("/config/video.properties");
            writer.write("[]");
        }

        FileReader reader = new FileReader("/config/video.properties");
        System.out.println(reader.readString());

        List<VideoEntity> videoEntityList = JSONUtil.toList((reader.readString()), VideoEntity.class);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "请求成功");
        map.put("count", videoEntityList.size());
        map.put("data", videoEntityList);

        return map;
    }

    @DeleteMapping("/delete")
    public Object del(@RequestParam("id") String id) {
        System.out.println(id);

        FileReader reader = new FileReader("/config/video.properties");
        System.out.println(reader.readString());

        List<VideoEntity> videoEntityList = JSONUtil.toList((reader.readString()), VideoEntity.class);
        videoEntityList.removeIf(videoEntity -> videoEntity.getId().equals(id));
        FileWriter writer = new FileWriter("/config/video.properties");
        writer.write(JSONUtil.toJsonStr(videoEntityList));
        return ResponseRest.success();
    }


    @PutMapping("/update")
    public ResponseRest update(@RequestBody VideoEntity videoEntity){

        System.out.println(videoEntity);

        FileReader reader = new FileReader("/config/video.properties");
        System.out.println(reader.readString());

        List<VideoEntity> buttonList = JSONUtil.toList((reader.readString()), VideoEntity.class);
        buttonList.removeIf(button -> button.getId().equals(videoEntity.getId()));
        buttonList.add(videoEntity);

        FileWriter writer = new FileWriter("/config/video.properties");
        writer.write(JSONUtil.toJsonStr(buttonList));


        return ResponseRest.success();
    }
}
